home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / cavants.zip / VANTS.TXT
Text File  |  1993-05-12  |  11KB  |  333 lines

  1. FROM:    INTERNET:A.Fraser@eee.salford.ac.uk, INTERNET:A.Fraser@eee.salford.ac.uk
  2. TO:    Mitch Allen, 76040,2757
  3.     (unknown), 100010,2243
  4. DATE:    05/11/93 11:26 AM
  5.  
  6. Re:    Virtual Ants in C
  7.  
  8. Sender: cellular-automata-request@Think.COM
  9. Received: from Mail.Think.COM by ihc.compuserve.com (5.65/5.930129sam)
  10.     id AA08046; Tue, 11 May 93 11:25:23 -0400
  11. Received: by mail.think.com; Tue, 11 May 93 11:21:11 -0400
  12. Return-Path: <A.Fraser@eee.salford.ac.uk>
  13. Received: from Think.COM by mail.think.com; Tue, 11 May 93 11:21:03 -0400
  14. Received: from europa.salford.ac.uk ([146.87.3.2]) by Early-Bird.Think.COM; Tue, 11 May 93 11:20:39 EDT
  15. Message-Id: <9305111520.AA09872@Early-Bird.Think.COM>
  16. Received: from mailgate-0.salford.ac.uk by europa.salford.ac.uk with SMTP (PP);
  17.           Tue, 11 May 1993 15:47:45 +0100
  18. From: A.Fraser@eee.salford.ac.uk
  19. Date: 11 May 93 10:27
  20. To: ca@Think.COM
  21. Subject: Virtual Ants in C
  22. X-Mailer: University of Salford cc:Mail/SMTP gateway 1.35
  23. Encoding: 306 TEXT
  24.  
  25. Hi,
  26.  
  27.      Continuing in the longest messages known to man here is a C
  28. implementation/port of the Virtual Ant program runs in Borland C but should be
  29. simply portable to other compilers/systems. If you do make a port please send
  30. me a copy, a Unix derivation would be nice.
  31.  
  32.      Also there is a new function which has 8 possible movement directions. The
  33. emergent function of this is like a pulsating protoplasm, very unusual!!!?
  34.  
  35.      This C port runs about 3/4 times faster than the Qbasic version and it is
  36. worthwhile to leave it for half an hour.
  37.  
  38.      Multiple Virtual Ants coming soon....
  39.  
  40.      Bugs and enhancements welcome.....
  41.  
  42. Ta ra
  43.      Adam P.Fraser
  44.  
  45.  code follows.....
  46.  
  47. /* *************************************************************************
  48.          **** Virtual Ant Code Version 1.0 10-11 May 1993 by Adam P.Fraser....**
  49. **
  50.          ***********************************************************************
  51. **/
  52.  
  53.  
  54. /* Compiled with Borland 3.1 but should be easily portable......... */
  55.  
  56. /*  This is a (near) direct port of a QBasic program written by Charles Wells
  57.                 Department of Mathematics, Case Western Reserve University.
  58.                  His notes follow....
  59.  
  60.                         "This program implements Chris Langton's cellular automa
  61. ton
  62.                 described in the Mathematical Intelligencer, volume 15, number 2
  63.                 (Spring 1993), page 54.  It describes the path of an ant who
  64.                 starts pointing in a certain direction.  If the ant is on a
  65.                 non-white square it turns the square red, rotates 90 degrees
  66.                 clockwise and moves one pixel in the direction it is pointing.
  67.                 If it is on a red square it turns the square white, rotates 90
  68.                 degrees counterclockwise and moves one pixel in the direction it
  69.                 is pointing.  In this implementation the path of the ant wraps
  70.                 around when it reaches the edge of the screen.  Most amazingly
  71.                 these simple rules produce behavior which looks chaotic for
  72.                 THOUSANDS of moves, then goes into a steady state during which
  73.                 it runs off the screen."
  74.  
  75.  
  76.                 Now my bit, actually this is more than a port there are a few mo
  77. re bells
  78.                 and whistles and it will (fingers crossed) run on a CGA monitor.
  79.                 On a 486dx33 it goes like a bat out of hell and is really quite 
  80. beautiful.
  81.  
  82.                 Try it on its own, just compile and run and watch.. the steady s
  83. tate
  84.                 quickly collides and a sort of 'rust' breaks out....
  85.  
  86.                 Try running Virtual_Ant2 instead of Virtual_Ant that is very une
  87. xpected
  88.                 you get something like a pulsating amoeba...
  89.  
  90.                 All the random lines and dots will produce wierd and wonderful e
  91. ffects.
  92.                 Try them and experiment.....
  93.  
  94.                 To finish just press any key....
  95.  
  96.                 Enjoy........
  97.  
  98.  
  99.                 All bugs and enhancements will be lovingly treated by me. (I wou
  100. ld love
  101.                 to see some really exciting new virtual ant code. How about the 
  102. turmite
  103.                 code Chris Langton wherever you are.....)
  104.  
  105.                 Coming soon MULTIPLE VIRTUAL ANTS !!!!.....
  106.  
  107.                         ta ra,
  108.                                                         Adam P. Fraser
  109.                                 Snail:Postgraduate Section         Email:a.frase
  110. r@eee.salford.ac.uk
  111.                                                         Maxwell Building
  112.                                                         University Of Salford
  113.                                                         M5 4WT
  114.                                                         England
  115.                                                                                 
  116.                                                                                 
  117.                                                                                 
  118.                                                          */
  119.  
  120. #include <graphics.h>
  121. #include <stdlib.h>
  122. #include <stdio.h>
  123. #include <conio.h>
  124. #include <time.h>
  125.  
  126. /* #define CGA    1  */                 /* Choose one and only one, thanks */
  127. /* #define EGA    3  */     /*  Note no SuperVGA mode yet   */
  128. #define VGA    9
  129.  
  130. #ifdef CGA
  131.         #define HORIZONTAL 320
  132.         #define VERTICAL   200
  133.         #define BACKGROUND BLACK
  134.         #define FOREGROUND CGA_RED
  135.         #define GRAPHICS   1
  136.         #define GMODE   CGAC2
  137. #endif
  138.  
  139. #ifdef EGA
  140.         #define HORIZONTAL 640
  141.         #define VERTICAL   350
  142.         #define BACKGROUND BLACK
  143.         #define FOREGROUND YELLOW
  144.         #define GRAPHICS   EGA
  145.         #define GMODE      EGAHI
  146. #endif
  147.  
  148. #ifdef VGA
  149.         #define HORIZONTAL 640
  150.         #define VERTICAL   480
  151.         #define BACKGROUND BLACK
  152.         #define FOREGROUND YELLOW
  153.         #define GRAPHICS    9
  154.         #define GMODE   VGAHI
  155. #endif
  156.  
  157.  
  158. /* Function Prototypes....................................................*/
  159.  
  160. void Random_Dots(int);
  161. void Random_Lines(int);
  162. void Random_Circles(int);
  163. double Virtual_Ant(void);
  164. double Virtual_Ant2(void);
  165.  
  166.  
  167. /* Yes its the main block of code */
  168.  
  169. void main()
  170. {
  171. /* The graphics drivers nicked straight from borland C example */
  172.         int gdriver = GRAPHICS, gmode=GMODE, errorcode;
  173.         double count;
  174.  
  175.         initgraph(&gdriver, &gmode, "");
  176.         errorcode = graphresult();
  177.         if (errorcode != grOk)           /* an error occurred */
  178.         {
  179.                 printf("Graphics error: %s\n", grapherrormsg(errorcode));
  180.                 printf("Press any key to halt:");
  181.                 getch();
  182.                 exit(1);                      /* return with error code */
  183.         }
  184.  
  185. /*      Random_Dots(100);     */        /* Any of these will give further fun */
  186. /*      Random_Lines(100);    */
  187. /*      Random_Circles(20);   */
  188.  
  189.         count=Virtual_Ant();             /* Main block of code */
  190. /*      count=Virtual_Ant2();  */     /* A new function takes place of above */
  191.  
  192.         printf("Iterations: %g\n",count);
  193.         printf("Please press a key");
  194.         {char c=getch();}               /* Buffer for chars will give an warning
  195.  */
  196.         while (!(kbhit()));
  197.         closegraph();
  198. }
  199.  
  200.  
  201.  
  202. void Random_Dots(int number_of_dots)
  203. {
  204.         int i,u,v;
  205.         randomize();      /* necessary to really mess up the pot */
  206.         for (i=0; i<=number_of_dots; i++)
  207.         {
  208.                 u=random(HORIZONTAL);
  209.                 v=random(VERTICAL);
  210.                 putpixel(u,v,FOREGROUND);
  211.         }
  212. }
  213.  
  214. void Random_Lines(int number_of_lines)
  215. {
  216.         int i,u,v,w,z;
  217.         randomize();             /* necessary to really mess up the pot */
  218.         setcolor(FOREGROUND);
  219.         for (i=0; i<=number_of_lines; i++)
  220.         {
  221.                 u=random(HORIZONTAL);
  222.                 v=random(VERTICAL);
  223.                 w=random(HORIZONTAL);
  224.                 z=random(VERTICAL);
  225.                 line(u,v,w,z);
  226.         }
  227. }
  228.  
  229.  
  230. void Random_Circles(int number_of_circles)
  231. {
  232.         int i,u,v,size;
  233.         randomize();            /* necessary to really mess up the pot */
  234.         setcolor(FOREGROUND);
  235.         for (i=0; i<=number_of_circles; i++)
  236.         {
  237.                 u=random(HORIZONTAL);
  238.                 v=random(VERTICAL);
  239.                 size=random(20);
  240.                 circle(u,v,size);
  241.         }
  242. }
  243.  
  244. /* The main ported code (not hacked, just got it to work) see initial note
  245.          to see what is going on....................                    APF 10/0
  246. 5/93       */
  247.  
  248. double Virtual_Ant(void)
  249. {
  250.         int vector=0,ht=VERTICAL,wdth=HORIZONTAL,y=ht/2,x=wdth/2;
  251.         int xdirs[4],ydirs[4];
  252.         double count=0;
  253.  
  254.         xdirs[0]=-1;xdirs[1]=0;xdirs[2]=1;xdirs[3]=0;
  255.         ydirs[0]=0;ydirs[1]=1;ydirs[2]=0;ydirs[3]=-1;
  256.  
  257.         while (!(kbhit()))
  258.         {
  259.                 if (x < 0) x=wdth-1;
  260.                 if (x > wdth-1) x=0;
  261.                 if (y < 0) y=ht-1;
  262.                 if (y > ht-1) y=0;
  263.  
  264.                 count++;
  265.  
  266.                 if ( getpixel(x,y) == BACKGROUND)
  267.                 {
  268.                         putpixel(x,y,FOREGROUND);
  269.                         vector=(vector+1)%4;      // rotation direction clockwis
  270. e
  271.                         x=x+xdirs[vector];
  272.                         y=y+ydirs[vector];
  273.                 }
  274.                 else
  275.                 {
  276.                         putpixel(x,y,BACKGROUND);
  277.                         vector=(vector+3)%4;      // rotation direction anti-clo
  278. ckwise
  279.                         x=x+xdirs[vector];
  280.                         y=y+ydirs[vector];
  281.                 }
  282.         }
  283.         return count;
  284. }
  285.  
  286. /* Basically the only difference between this and Virtual_Ant() is that
  287.          this can move in 8 directions NE,SW etc (in Cellular Automata terms
  288.          instead if having 5 neighbourhood states we have nine). The change to
  289.          the program though is quite amazing........... APF 11/05/93   */
  290.  
  291. double Virtual_Ant2(void)
  292. {
  293.         int vector=0,ht=VERTICAL,wdth=HORIZONTAL,y=ht/2,x=wdth/2;
  294.         int xdirs[8],ydirs[8];
  295.         double count=0;
  296.  
  297.         xdirs[0]=-1;xdirs[1]=-1;xdirs[2]=0;xdirs[3]=1;
  298.         xdirs[4]=1;xdirs[5]=1;xdirs[6]=0;xdirs[7]=-1;
  299.  
  300.         ydirs[0]=0;ydirs[1]=1;ydirs[2]=1;ydirs[3]=1;
  301.         ydirs[4]=0;ydirs[5]=-1;ydirs[6]=-1;ydirs[7]=-1;
  302.  
  303.         while (!(kbhit()))
  304.         {
  305.                 if (x < 0) x=wdth-1;
  306.                 if (x > wdth-1) x=0;
  307.                 if (y < 0) y=ht-1;
  308.                 if (y > ht-1) y=0;
  309.  
  310.                 count++;
  311.  
  312.                 if ( getpixel(x,y) == BACKGROUND)
  313.                 {
  314.                         putpixel(x,y,FOREGROUND);
  315.                         vector=(vector+1)%8;      // rotation direction clockwis
  316. e
  317.                         x=x+xdirs[vector];
  318.                         y=y+ydirs[vector];
  319.                 }
  320.                 else
  321.                 {
  322.                         putpixel(x,y,BACKGROUND);
  323.                         vector=(vector+7)%8;      // rotation direction anti-clo
  324. ckwise
  325.                         x=x+xdirs[vector];
  326.                         y=y+ydirs[vector];
  327.                 }
  328.         }
  329.         return count;
  330. }
  331.  
  332.  
  333.